﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace APIBuilder
{
    public partial class NewAPI : Form
    {
        public NewAPI()
        {
            InitializeComponent();
            this.Icon = new Icon("Resources/ICO.ico");
            this.MaximizeBox = false;
        }



        /// <summary>
        /// 创建按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CreateBtn_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(Mname.Text.Trim())|| string.IsNullOrEmpty(MSpace.Text.Trim()))
            {
                MessageBox.Show("Shitttter!全部都得填！", Common.Caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                //生成初始文档
                string templateURL = Common.GetBaseURL() + Common.GetConfigByKey("TemplateURL");
                if (File.Exists(templateURL))
                {
                    string template = File.ReadAllText(Common.GetBaseURL() + Common.GetConfigByKey("TemplateURL"), Encoding.UTF8);
                    string newHtml = template.Replace("{$Title}", MSpace.Text.Trim()).Replace("<!--HTMLHolder-->", string.Format("<p class='p1'>{0}({1})</p><!--HTMLHolder-->", Mname.Text.Trim(), MSpace.Text.Trim()));
                    File.WriteAllText(Common.GetBaseURL() + Common.GetConfigByKey("APIURL") + MSpace.Text.Trim() + ".html", newHtml, Encoding.UTF8);
                    //更新Indexs缓存
                    Common.NewAPI = MSpace.Text.Trim();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Shitttter!找不到模板！", Common.Caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
        }
    }
}
